﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SetSailModel;
using Dapper;

namespace SetSailDal
{
    /// <summary>
    /// 登录、注册
    /// </summary>
    public static class LoginDal
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="ly"></param>
        /// <returns></returns>
        public static Result<LH_YHXX> Login(LH_YHXX ly)
        {
            Result<LH_YHXX> res = new Result<LH_YHXX>();
            try
            {
                string sql = string.Format(@"SELECT * 
                                               FROM LH_YHXX AS ly WITH(NOLOCK)
                                              WHERE ly.DLZH='{0}' AND ly.DLMM='{1}' AND ly.SCBJ=0", ly.DLZH, ly.DLMM);
                using (DBHelper help = new DBHelper())
                {
                    res.Data = help.DbContext.QueryFirstOrDefault<LH_YHXX>(sql);
                    if (res.Data == null)
                    {
                        res.Code = ResultCode.Fail;
                        res.Message = "账号或密码错误";
                    }
                    else
                    {
                        if (res.Data.DLZT == "禁止登录")
                        {
                            res.Code = ResultCode.Fail;
                            res.Message = "该账号禁止登录";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                res.Code = ResultCode.Exception;
                res.Message = ex.Message;
            }
            return res;
        }


        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="ly"></param>
        /// <returns></returns>
        public static Result<int> Register(LH_ZCSH ly)
        {
            Result<int> res = new Result<int>();
            try
            {
                string sql = string.Format(@"SELECT * FROM LH_ZCSH AS lz WITH(NOLOCK) WHERE lz.ZCZH='{0}' AND lz.SCBJ=0", ly.ZCZH);
                string sql_add = string.Format(@"INSERT INTO LH_ZCSH
                                                (
                                                	ID,
                                                	ZCZH,
                                                    ZCMM,
                                                	YY,
                                                	QQ,
                                                	SQSJ,
                                                	SCBJ
                                                )
                                                VALUES
                                                (
                                                	REPLACE(NEWID(),'-',''),
                                                	@ZCZH,
                                                	@ZCMM,
                                                	@YY,
                                                	@QQ,
                                                	GETDATE(),
                                                	0
                                                )");
                using (DBHelper help = new DBHelper())
                {
                    LH_ZCSH lz = help.DbContext.QueryFirstOrDefault<LH_ZCSH>(sql);
                    if (lz != null)
                    {
                        res.Code = ResultCode.Fail;
                        res.Message = "注册失败,注册账号已存在";
                    }
                    else
                    {
                        res.Count = help.DbContext.Execute(sql_add, ly);
                        if (res.Count == 0)
                        {
                            res.Code = ResultCode.Fail;
                            res.Message = "注册失败,请联系管理员";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                res.Code = ResultCode.Exception;
                res.Message = ex.Message;
            }
            return res;
        }
    }
}
